package src.data;



import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;

import src.domain.IncomingMessage;
import src.domain.MailBox;



public class IncomingMessageDAO {

	private HibernateProvider provider;

	private static IncomingMessageDAO instance = null;

	public static IncomingMessageDAO getInstance() {
		if (instance == null)
			instance = new IncomingMessageDAO();
		return instance;
	}

	private IncomingMessageDAO() {
		this.provider = HibernateProvider.getInstance();

	}
	
	public IncomingMessage getByID(IncomingMessage incomingMessage){
		Criteria criteria = provider.getSession().createCriteria(IncomingMessage.class).add(Restrictions.like("id",incomingMessage.getId()));
		return (IncomingMessage) criteria.uniqueResult();	
	}

	public void saveOrUpdate(IncomingMessage incomingMessage) {
		provider.getSession().saveOrUpdate(incomingMessage);
	}
	
	public void delete (IncomingMessage incomingMessage) {
		provider.getSession().delete(incomingMessage);
	}
	
	
	public MailBox getMailBoxeByIncomingMessage(IncomingMessage incomingMessage) {
		Query query = provider.getSession().createQuery("select m from MailBox m left join m.incomingMessages as i where i = :i");
		query.setParameter("i",incomingMessage);
		return (MailBox)query.uniqueResult();

	}

}
